<?php

namespace app\api\controller\xiluhouse;

use app\common\controller\Api;
use app\common\model\xiluhouse\Article as ArticleModel;
use app\common\model\xiluhouse\ArticleCategory as ArticleCategoryModel;
use app\common\model\xiluhouse\QAndA as QAndAModel;
use app\common\model\xiluhouse\QAndALike as QAndALikeModel;
use app\common\model\xiluhouse\QAndATags as QAndATagsModel;
use app\common\model\xiluhouse\HouseTrends as HouseTrendsModel;
use think\Db;

/**
 * @title   文章类
 * @desc    获取资讯、楼市头条、文章单页的接口
 */
class Article extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['article_list', 'article_category_data', 'article_detail', 'share_qrcode', 'service_list'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = '*';

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ArticleModel();
    }

    /**
     * @title  文章（楼市头条、资讯、我的权益）
     * @desc  获取-楼市头条、资讯、我的权益
     *
     * @param int    $classify             2            分类:1=单页分类,2=楼市头条,3=资讯,4=买房百科，5=我的权益
     * @param int    $article_category_id  0            分类id
     * @param int    $sort                 0            排序:0=排序字段从大到小，1=创建时间从大到小，2=按热度从大到小
     *
     */
    public function article_list()
    {
        $map['classify'] = $this->request->param('classify/d', 2);
        $article_category_id = $this->request->param('article_category_id/d', 0);
        if ($article_category_id) {
            $map['article_category_id'] = $article_category_id;
        }
        $sort = $this->request->param('sort/d', 0);
        $order = [];
        switch ($sort) {
            case 1:
                # 创建时间从大到小
                $order = ['id' => 'desc', 'weigh' => 'desc'];
                break;
            case 2:
                # 按热度从大到小
                $order = ['view_num' => 'desc', 'weigh' => 'desc', 'id' => 'desc'];
                break;
            default:
                # 排序字段从大到小
                $order = ['weigh' => 'desc', 'id' => 'desc'];
                break;
        }
        // $list=$this->model->getListByMap($map,[],$order);
        $map['status'] = 'normal';
        $list = $this->model->where($map)->order($order)->paginate();

        $this->success('', $list);
    }

    /**
     * @title  买房百科
     * @desc  买房百科
     *
     *
     */
    public function service_list()
    {
        $map['article.classify'] = $this->request->param('classify/d', 4);
        $sort = $this->request->param('sort/d', 0);
        $order = ['articlecategory.weigh' => 'desc', 'article.weigh' => 'desc', 'id' => 'desc'];
        $map['article.status'] = 'normal';
        $map['articlecategory.status'] = 'normal';
        $data = $this->model->with(['articlecategory'])->where($map)->order($order)->select();
        $now_data = [];
        foreach ($data as $key => $value) {
            $now_data[$value['articlecategory']['name']][] = $value;
        }
        $data2 = [];
        foreach ($now_data as $key => $value) {
            $data2[] = [
                'articlecategory_name' => $key,
                'data' => $value,
            ];
        }
        $this->success('', $data2);
    }

    /**
     * @title  分类
     * @desc  获取 楼市头条、资讯的分类
     *
     * @param int    $classify             2            分类:1=单页分类,2=楼市头条,3=资讯
     *
     */
    public function article_category_data()
    {
        $model = new ArticleCategoryModel;
        $map['classify'] = $this->request->param('classify/d', 2);
        $data = $model->getDataByMap($map);
        $this->success('', $data);
    }

    /**
     * @title  文章详情（楼市头条、资讯、我的权益、买房百科）
     * @desc  获取 楼市头条、资讯、我的权益、买房百科的详细信息
     *
     * @param int    $id             0            文章id
     *
     */
    public function article_detail()
    {
        $id = $this->request->param('id/d', 0);
        if (!$id) {
            $this->error('参数错误');
        }
        // $info=$this->model->getInfoByMap(['id'=>$id]);
        $info = $this->model->where('status', 'normal')->find($id);
        if (!$info) {
            $this->error('文章不存在');
        }
        $info->setInc('view_num');
        $this->success('', $info);
    }

    /**
     * @title  分享时需要获取小程序码
     * @desc  分享时需要获取小程序码
     *
     * @param int    $id             0            文章id
     *
     */
    public function share_qrcode()
    {
        $id = $this->request->param('id/d', 0);
        if (!$id) {
            $this->error('参数错误');
        }
        $qrcodemodel = new \app\common\model\xiluhouse\ArticleQrcode;
        $exist = $qrcodemodel->where('article_id', $id)->where('user_id', $this->auth->id)->find();
        if ($exist) {
            $this->success('', cdnurl($exist['url'],true));
        }
        $wxmini = new \app\common\library\xiluhouse\Wxmini;
        $res = $wxmini->getWXacode('user_' . $this->auth->id . ',article_' . $id, 'pages/article/info_detail/info_detail');
        if(!$res) $this->error('获取小程序二维码失败');
        $root = '.';
        $filename = md5(mt_rand()).'.png';
        $path = "/uploads/".date('Ymd').'/';
        if (!file_exists($root . $path)) {
            @mkdir($root . $path, 0777, true);
        }
        $qrcode = $path.$filename;
        if(!get_addon_config('alioss')){
            file_put_contents($root.$qrcode,$res);
        }else{
            Common::uploadAlioss($path, $res);
        }
        if ($qrcode) {
            $qrcodemodel->insert(['article_id' => $id, 'user_id' => $this->auth->id, 'url' => $qrcode]);
        }
        if(!$qrcode){
            $qrcode = \app\common\model\xiluhouse\Config::getdatas('wxmini_logo');
        }
        $share_qrcode_text = cdnurl($qrcode,true);
        $this->success('', $share_qrcode_text);
    }
}
